package com.ysh.blog.controller;

import com.ysh.blog.mapper.Article;
import com.ysh.blog.pojo.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

@Controller
public class BolgController {
    @Autowired
    Article article;

    @RequestMapping("/")
    public String index(Model model,HttpServletResponse response,Integer page,String count) {
        //model.addAttribute("name", "simonsfan");
        if(count==null || count.equals("") || count.length()==0){
            count="5";
        }

        Page p=new Page(page ,Integer.parseInt(count));
       // List<HashMap<Object,Object>> list= article.list();
        Integer counts=article.count();

        List<HashMap<Object,Object>> list2= article.listPage(p.getStart(),p.getCount());


        model.addAttribute("list", list2);
        int c=(counts+p.getCount()-1)/p.getCount();
        model.addAttribute("pageCount", c);
        model.addAttribute("count", Integer.parseInt(count));
        model.addAttribute("page", page);
       // model.addAttribute("list", list);
        System.out.println("==============");
        return "/client/index";
    }


    @RequestMapping("/index2")
    @ResponseBody
    public String index2() {
        System.out.println("==============");
        return "eserjserjseiresirie";
    }

}
